# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>
# ArchLinux maintainer: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: dibblethewrecker dibblethewrecker.at.jiwe.dot.org
# Contributor: William Rea <sillywilly@gmail.com>
# Contributor: Petri Moisio <petri.k.moisio@gmail.com>

_realname=gdal
pkgbase=mingw-w64-${_realname,,}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=3.10.3
pkgrel=2
pkgdesc="A translator library for raster geospatial data formats (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gdal.org/"
msys2_repository_url="https://github.com/OSGeo/gdal"
msys2_references=(
  'archlinux: gdal'
  "cpe: cpe:/a:gdal:gdal"
  "cpe: cpe:/a:osgeo:gdal"
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-opencl-headers"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-xsimd")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-armadillo"
         "${MINGW_PACKAGE_PREFIX}-arrow"
         "${MINGW_PACKAGE_PREFIX}-blosc"
         "${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-crypto++"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-hdf4"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-lerc"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libdeflate"
         "${MINGW_PACKAGE_PREFIX}-libfreexl"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libjxl"
         "${MINGW_PACKAGE_PREFIX}-libkml"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-opencl-icd"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-podofo")
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-sfcgal"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         #"${MINGW_PACKAGE_PREFIX}-tiledb"
         "${MINGW_PACKAGE_PREFIX}-xerces-c"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=(https://github.com/OSGeo/gdal/releases/download/v${pkgver}/gdal-${pkgver}.tar.gz
        001-fix-bindings-install-path.patch
        002-remove-manual-soversion.patch
        https://github.com/OSGeo/gdal/commit/e5c4ae5d6eb1f478af9e1604bb7dfdb854862a83.patch)
sha256sums=('e4bf7f104acbcb3e2d16c97fd1af2b92b28d0ba59d17d976e3ef08b794f4153b'
            'bfb0b42c8562bd45ae5cff71d42b042e398dff9a9c1f5bb08d8717a7753d4f3f'
            'fc815a58fe229db3c44bae15f405cafbeb82de91ec3f35564569c5bbecb1c3d7'
            '97be9c8477831eb3d73c47e62ad1339691c86af2b5ae20ad2d367a2ba2551c2e')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    001-fix-bindings-install-path.patch \
    002-remove-manual-soversion.patch \
    e5c4ae5d6eb1f478af9e1604bb7dfdb854862a83.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} == x86_64 ]]; then
    _extra_config+=("-DGDAL_USE_PODOFO=ON")
  else
    _extra_config+=("-DGDAL_USE_PODOFO=OFF")
  fi

  msg2 "Building static library"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DCMAKE_CXX_STANDARD=17 \
      -DBUILD_PYTHON_BINDINGS=OFF \
      -DGDAL_USE_INTERNAL_LIBS=OFF \
      -DGDAL_USE_HDFS=OFF \
      -DGDAL_USE_MSSQL_ODBC=OFF \
      -DGDAL_USE_POPPLER=ON \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  msg2 "Building shared library"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DCMAKE_CXX_STANDARD=17 \
      -DBUILD_PYTHON_BINDINGS=ON \
      -DBUILD_JAVA_BINDINGS=OFF \
      -DGDAL_USE_INTERNAL_LIBS=OFF \
      -DGDAL_USE_HDFS=OFF \
      -DGDAL_USE_MSSQL_ODBC=OFF \
      -DGDAL_USE_POPPLER=ON \
      -DBUILD_TESTING=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check () {
  cd "${srcdir}/build-${MSYSTEM}-shared"

  "${MINGW_PREFIX}"/bin/cmake.exe -DBUILD_TESTING=ON ../${_realname}-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake.exe --build .
  "${MINGW_PREFIX}"/bin/ctest.exe .
}

package_gdal() {
  msg2 "Installing static library"
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  msg2 "Installing shared library"
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  # install license
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # Move python stuff
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared/{bin,lib}
  mv "${pkgdir}"${MINGW_PREFIX}/bin/*py "${srcdir}"/build-${MSYSTEM}-shared/bin
  mv "${pkgdir}"${MINGW_PREFIX}/lib/python* "${srcdir}"/build-${MSYSTEM}-shared/lib
}

package_python-gdal() {
  pkgdesc="Python bindings for GDAL (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-python-psycopg2"
           "${MINGW_PACKAGE_PREFIX}-python-numpy")

  msg2 "Installing Python bindings"
  cd "${srcdir}/build-${MSYSTEM}-shared"

  install -d "${pkgdir}"${MINGW_PREFIX}/{bin,lib}
  mv "${srcdir}"/build-${MSYSTEM}-shared/bin/* "${pkgdir}"${MINGW_PREFIX}/bin
  mv "${srcdir}"/build-${MSYSTEM}-shared/lib/* "${pkgdir}"${MINGW_PREFIX}/lib

  # fix python command in files
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
